//
//  YDStockMarketPopSelectedController.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2018/1/22.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDStockMarketPopSelectedController.h"
#import "YDStockMarketPopSelectedCell.h"
#import "YDHomeMarketModel.h"

@interface YDStockMarketPopSelectedController ()<UITableViewDelegate , UITableViewDataSource>


//tableView
@property (nonatomic , weak) UITableView *tableView;

@end

@implementation YDStockMarketPopSelectedController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIColor *color = [UIColor clearColor];
    self.view.backgroundColor = [color colorWithAlphaComponent:0.5];
    
    [self tableView];
}
-(void)dealloc
{
    DDLog(@"释放了")
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.delegate && [self.delegate respondsToSelector:@selector(dissmissWithoutSelected)]) {
            [self.delegate dissmissWithoutSelected];
        }
    }];
}
-(UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame;
        CGFloat margin;
        if (isPhoneX) {
            frame = CGRectMake(0, 90, rate375(270), rateX812(176));
            margin = rateX812(8);
        }else{
            frame = CGRectMake(0, 66, rate375(270), rate750(176));
            margin = rate750(8);
        }
        UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.showsHorizontalScrollIndicator = NO;
        tableView.scrollEnabled = NO;
        tableView.centerX = self.view.centerX;
        tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"celuexuanze-bg"]];
        tableView.contentInset = UIEdgeInsetsMake(margin, 0, 0, 0);
        _tableView = tableView;
    }
    return _tableView;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return isPhoneX ? rateX812(56) : rate750(56);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDStockMarketPopSelectedCell *cell = [YDStockMarketPopSelectedCell cellWithTableView:tableView];
    YDHomeMarketModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.startIndex) {
        [self dismissViewControllerAnimated:YES completion:^{
            if (self.delegate && [self.delegate respondsToSelector:@selector(dissmissWithoutSelected)]) {
                [self.delegate dissmissWithoutSelected];
            }
        }];
    }else{
        for (YDHomeMarketModel *model in self.modelArray) {
            model.isSelected = NO;
        }
        
        YDHomeMarketModel *model = self.modelArray[indexPath.row];
        model.isSelected = YES;
        [self.tableView reloadData];
        
        [self dismissViewControllerAnimated:YES completion:^{
            if (self.delegate && [self.delegate respondsToSelector:@selector(stockMarketPopSelectedController:didSelectedWhichStock:)]) {
                [self.delegate stockMarketPopSelectedController:self didSelectedWhichStock:indexPath.row];
            }
        }];
        
        
    }

    
}



@end
